package ar.edu.uba.fi.jmit.monitorengine.backend.client.view;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class ErrorDialog extends DialogBox implements ClickHandler {
	
	private Button buttonClose;
	
	public ErrorDialog(String message, String detailesMessage) {
		super(false, true);
		this.setTitle(message);
		this.setText(message);
		Panel panel = new VerticalPanel();
		panel.add(new HTML(detailesMessage));
		panel.add(createButtonsPanel());
		this.add(panel);
		this.center();
	}
	
	public ErrorDialog(String message, StackTraceElement[] stackTrace) {
		super(false, true);
		this.setTitle(message);
		this.setText(message);
		Panel panel = new VerticalPanel();
		panel.add(new HTML(this.createStackTraceHtml(stackTrace)));
		panel.add(createButtonsPanel());
		this.add(panel);
		this.center();
	}
	
	public ErrorDialog(String message, String detailedMessage, StackTraceElement[] stackTrace) {
		super(false, true);
		this.setTitle(message);
		this.setText(message);
		Panel panel = new VerticalPanel();
		panel.add(new Label(detailedMessage));
		panel.add(new HTML(this.createStackTraceHtml(stackTrace)));
		panel.add(createButtonsPanel());
		this.add(panel);
		this.center();
	}

	private String createStackTraceHtml(StackTraceElement[] stackTrace) {
		StringBuffer buff = new StringBuffer();
		for(StackTraceElement element:stackTrace) {
			buff.append(element.getClassName() + "." + element.getMethodName() + "()" + " [" + 
					element.getLineNumber() + "]<br/>");
		}
		return buff.toString();
	}
	
	private Widget createButtonsPanel() {
		Panel panel = new HorizontalPanel();
		buttonClose = new Button("Close");
		panel.add(buttonClose);
		buttonClose.addClickHandler(this);
		return panel;
	}

	@Override
	public void onClick(ClickEvent arg0) {
		this.hide();
	}

}
